home *** CD-ROM | disk | FTP | other *** search
/ Java Interactive Reference Guide / Java Interactive Reference Guide.iso / autorun / source.dir / 00078_15.txt < prev    next >
Encoding:
Text File  |  1980-01-11  |  10.7 KB  |  329 lines

  1. /*
  2.  * @(#)ImageMapArea.java    1.3 95/10/13  
  3.  *
  4.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  8.  * without fee is hereby granted. 
  9.  * Please refer to the file http://java.sun.com/copy_trademarks.html
  10.  * for further important copyright and trademark information and to
  11.  * http://java.sun.com/licensing.html for further important licensing
  12.  * information for the Java (tm) Technology.
  13.  * 
  14.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  15.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  16.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  18.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  19.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  20.  * 
  21.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  22.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  23.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  24.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  25.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  26.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  27.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  28.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  29.  * HIGH RISK ACTIVITIES.
  30.  */
  31.  
  32. import java.awt.Graphics;
  33. import java.awt.Image;
  34. import java.awt.image.*;
  35. import java.util.StringTokenizer;
  36. import java.net.URL;
  37. import java.net.MalformedURLException;
  38.  
  39. /**
  40.  * The base ImageArea class.
  41.  * This class performs the basic functions that most ImageArea
  42.  * classes will need and delegates specific actions to the subclasses.
  43.  *
  44.  * @author     Jim Graham
  45.  * @version     1.3, 10/13/95
  46.  */
  47. class ImageMapArea implements ImageObserver {
  48.     /** The applet parent that contains this ImageArea. */
  49.     ImageMap parent;
  50.     /** The X location of the area (if rectangular). */
  51.     int X;
  52.     /** The Y location of the area (if rectangular). */
  53.     int Y;
  54.     /** The size().width of the area (if rectangular). */
  55.     int W;
  56.     /** The size().height of the area (if rectangular). */
  57.     int H;
  58.     /**
  59.      * This flag indicates whether the user was in this area during the
  60.      * last scan of mouse locations.
  61.      */
  62.     boolean entered = false;
  63.     /** This flag indicates whether the area is currently highlighted. */
  64.     boolean active = false;
  65.  
  66.     /**
  67.      * This is the default highlight image if no special effects are
  68.      * needed to draw the highlighted image.  It is created by the
  69.      * default "makeImages()" method.
  70.      */
  71.     Image hlImage;
  72.  
  73.     /**
  74.      * Initialize this ImageArea as called from the applet.
  75.      * If the subclass does not override this initializer, then it
  76.      * will perform the basic functions of setting the parent applet
  77.      * and parsing out 4 numbers from the argument string which specify
  78.      * a rectangular region for the ImageArea to act on.
  79.      * The remainder of the argument string is passed to the handleArg()
  80.      * method for more specific handling by the subclass.
  81.      */
  82.     public void init(ImageMap parent, String args) {
  83.     this.parent = parent;
  84.     StringTokenizer st = new StringTokenizer(args, ", ");
  85.     X = Integer.parseInt(st.nextToken());
  86.     Y = Integer.parseInt(st.nextToken());
  87.     W = Integer.parseInt(st.nextToken());
  88.     H = Integer.parseInt(st.nextToken());
  89.     if (st.hasMoreTokens()) {
  90.         // hasMoreTokens() Skips the trailing comma
  91.         handleArg(st.nextToken(""));
  92.     } else {
  93.         handleArg(null);
  94.     }
  95.     makeImages();
  96.     }
  97.  
  98.     /**
  99.      * This method handles the remainder of the argument string after
  100.      * the standard initializer has parsed off the 4 rectangular
  101.      * parameters.  If the subclass does not override this method,
  102.      * the remainder will be ignored.
  103.      */
  104.     public void handleArg(String s) {
  105.     }
  106.  
  107.     /**
  108.      * This method loads any additional media that the ImageMapArea
  109.      * may need for its animations.
  110.      */
  111.     public void getMedia() {
  112.     }
  113.  
  114.     /**
  115.      * This method is called every animation cycle if there are any
  116.      * active animating areas.
  117.      * @return true if this area requires further animation notifications
  118.      */
  119.     public boolean animate() {
  120.     return false;
  121.     }
  122.  
  123.     /**
  124.      * This method sets the image to be used to render the ImageArea
  125.      * when it is highlighted.
  126.      */
  127.     public void setHighlight(Image img) {
  128.     hlImage = img;
  129.     }
  130.  
  131.     /**
  132.      * This method handles the construction of the various images
  133.      * used to highlight this particular ImageArea when the user
  134.      * interacts with it.
  135.      */
  136.     public void makeImages() {
  137.     setHighlight(parent.getHighlight(X, Y, W, H));
  138.     }
  139.  
  140.     /**
  141.      * The repaint method causes the area to be repainted at the next
  142.      * opportunity.
  143.      */
  144.     public void repaint() {
  145.     parent.repaint(0, X, Y, W, H);
  146.     }
  147.  
  148.     /**
  149.      * This method tests to see if a point is inside this ImageArea.
  150.      * The standard method assumes a rectangular area as parsed by
  151.      * the standard initializer.  If a more complex area is required
  152.      * then this method will have to be overridden by the subclass.
  153.      */
  154.     public boolean inside(int x, int y) {
  155.     return (x >= X && x < (X + W) && y >= Y && y < (Y + H));
  156.     }
  157.  
  158.     /**
  159.      * This utility method draws a rectangular subset of a highlight
  160.      * image.
  161.      */
  162.     public void drawImage(Graphics g, Image img, int imgx, int imgy,
  163.               int x, int y, int w, int h) {
  164.     Graphics ng = g.create();
  165.     ng.clipRect(x, y, w, h);
  166.     ng.drawImage(img, imgx, imgy, this);
  167.     }
  168.  
  169.     /**
  170.      * This method handles the updates from drawing the images.
  171.      */
  172.     public boolean imageUpdate(Image img, int infoflags,
  173.                    int x, int y, int width, int height) {
  174.     if (img == hlImage) {
  175.         return parent.imageUpdate(img, infoflags, x + X, y + Y,
  176.                       width, height);
  177.     } else {
  178.         return (infoflags & (ALLBITS | ERROR)) == 0;
  179.     }
  180.     }
  181.  
  182.     /**
  183.      * This utility method shows a string in the status bar.
  184.      */
  185.     public void showStatus(String msg) {
  186.     parent.getAppletContext().showStatus(msg);
  187.     }
  188.  
  189.     /**
  190.      * This utility method tells the browser to visit a URL.
  191.      */
  192.     public void showDocument(URL u) {
  193.     parent.getAppletContext().showDocument(u);
  194.     }
  195.  
  196.     /**
  197.      * This method highlights the specified area when the user enters
  198.      * it with his mouse.  The standard highlight method is to replace
  199.      * the indicated rectangular area of the image with the primary
  200.      * highlighted image.
  201.      */
  202.     public void highlight(Graphics g) {
  203.     }
  204.  
  205.     /**
  206.      * The checkEnter method is called when the mouse is inside the
  207.      * region to see if the area needs to have its enter method called.
  208.      * The default implementation simply checks if the entered flag is
  209.      * set and only calls enter if it is false.
  210.      */
  211.     public boolean checkEnter(int x, int y) {
  212.     if (entered) {
  213.         return isTerminal();
  214.     } else {
  215.         entered = true;
  216.         return enter(x, y);
  217.     }
  218.     }
  219.  
  220.     /**
  221.      * The checkExit method is called when the mouse is outside the
  222.      * region to see if the area needs to have its exit method called.
  223.      * The default implementation simply checks if the entered flag is
  224.      * set and only calls exit if it is true.
  225.      */
  226.     public void checkExit() {
  227.     if (entered) {
  228.         entered = false;
  229.         exit();
  230.     }
  231.     }
  232.  
  233.     /**
  234.      * The isTerminal method controls whether events propagate to the
  235.      * areas which lie beneath this one.
  236.      * @return true if the events should be propagated to the underlying
  237.      * areas.
  238.      */
  239.     public boolean isTerminal() {
  240.     return false;
  241.     }
  242.  
  243.     /**
  244.      * The enter method is called when the mouse enters the region.
  245.      * The location is supplied, but the standard implementation is
  246.      * to call the overloaded method with no arguments.
  247.      * @return true if this ImageMapArea wants to prevent any underlying
  248.      * areas from seeing the enter
  249.      */
  250.     public boolean enter(int x, int y) {
  251.     return enter();
  252.     }
  253.  
  254.     /**
  255.      * The overloaded enter method is called when the mouse enters
  256.      * the region.  This method can be overridden if the ImageArea
  257.      * does not need to know where the mouse entered.
  258.      * @return true if this ImageMapArea wants to prevent any underlying
  259.      * areas from seeing the enter
  260.      */
  261.     public boolean enter() {
  262.     return isTerminal();
  263.     }
  264.  
  265.     /**
  266.      * The exit method is called when the mouse leaves the region.
  267.      */
  268.     public void exit() {
  269.     }
  270.  
  271.     /**
  272.      * The press method is called when the user presses the mouse
  273.      * button inside the ImageArea.  The location is supplied, but
  274.      * the standard implementation is to call the overloaded method
  275.      * with no arguments.
  276.      * @return true if this ImageMapArea wants to prevent any underlying
  277.      * areas from seeing the press
  278.      */
  279.     public boolean press(int x, int y) {
  280.     return press();
  281.     }
  282.  
  283.     /**
  284.      * The overloaded press method is called when the user presses the
  285.      * mouse button inside the ImageArea.  This method can be overridden
  286.      * if the ImageArea does not need to know the location of the press.
  287.      * @return true if this ImageMapArea wants to prevent any underlying
  288.      * areas from seeing the press
  289.      */
  290.     public boolean press() {
  291.     return isTerminal();
  292.     }
  293.  
  294.     /**
  295.      * The lift method is called when the user releases the mouse button.
  296.      * The location is supplied, but the standard implementation is to
  297.      * call the overloaded method with no arguments.  Only those ImageAreas
  298.      * that were informed of a press will be informed of the corresponding
  299.      * release.
  300.      * @return true if this ImageMapArea wants to prevent any underlying
  301.      * areas from seeing the lift
  302.      */
  303.     public boolean lift(int x, int y) {
  304.     return lift();
  305.     }
  306.  
  307.     /**
  308.      * The overloaded lift method is called when the user releases the
  309.      * mouse button.  This method can be overridden if the ImageArea
  310.      * does not need to know the location of the release.
  311.      * @return true if this ImageMapArea wants to prevent any underlying
  312.      * areas from seeing the lift
  313.      */
  314.     public boolean lift() {
  315.     return isTerminal();
  316.     }
  317.  
  318.     /**
  319.      * The drag method is called when the user moves the mouse while
  320.      * the button is pressed.  Only those ImageAreas that were informed
  321.      * of a press will be informed of the corresponding mouse movements.
  322.      * @return true if this ImageMapArea wants to prevent any underlying
  323.      * areas from seeing the drag
  324.      */
  325.     public boolean drag(int x, int y) {
  326.     return isTerminal();
  327.     }
  328. }
  329.